﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using LC;

namespace Scan
{
    public partial class TestNetworkView : BaseView
    {
        const string NetworkConnected = "网络已连接";
        const string NetworkNoConnected = "网络未连接";
        const string NetworkConnectFail = "网络连接失败";
        const string OFFLINE = "OffLine";
        const string Wifi = "WIFI";
        const string GPRS = "GPRS";

        public TestNetworkView()
        {
            InitializeComponent();
            this.title = "网路切换";
        }

        public override void OnLoad()
        {
            this.timer1.Enabled = true;
            rbGprs.Checked = (NetworkManager.ConnectType == ConnType.Gprs);
            rbWlan.Checked = (NetworkManager.ConnectType == ConnType.Wlan);
            RefreshNetStatus();
        }

        public override void S2(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            ConnType netType = rbGprs.Checked ? ConnType.Gprs : ConnType.Wlan;
            if (Device.CheckNetworkStat() && netType == NetworkManager.ConnectType)
            {
                RefreshNetStatus();
            }
            else
            {
                NetworkManager.ConnectType = rbGprs.Checked ? ConnType.Gprs : ConnType.Wlan;
                this.TransferTitle = String.Format("正在连接{0}网络，请稍后...", rbGprs.Checked ? "GPRS" : "Wifi");
                this.OnConnectStart();
            }
            timer1.Enabled = true;
        }

        public override void S1(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            this.ParentForm.LoadView(this.ResultView.GetType(), this, true, false);
        }

        protected override void OnConnectNotify(object sender, ConnectNotifyEventArgs e)
        {
            if (e.ConnectResult != ConnState.Connecting)
            {
                GlobalCache.Instance.NetworkMgr.ConnectNotify -= new ConnectNotifyEventHandler(this.ConnectNotify);
                this.ParentForm.HideProgress();
                this.Enabled = true;
                RefreshNetStatus();
            }
        }

        private void RefreshNetStatus()
        {
            switch (NetworkManager.ConnectType)
            {
                case ConnType.Wlan:
                    this.lblNetType.Text = Wifi;
                    break;
                case ConnType.Gprs:
                    this.lblNetType.Text = GPRS;
                    break;
                default:
                    this.lblNetType.Text = OFFLINE;
                    break;
            }
            if (Device.CheckNetworkStat())
            {
                this.lblNetStatus.Text = NetworkConnected;
                this.lblSignalStrength.Text = Device.GetWlanSignalStrength().ToString();
            }
            else
            {
                this.lblNetStatus.Text = NetworkNoConnected;
                this.lblSignalStrength.Text = "0";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RefreshNetStatus();
        }
    }
}
